// 厂家工程师给的其他裸机MCU的调试代码

#include "Mb96620.h"
#include "TP_Driver.h"
#include "I2c.h"
#include "Timer.h"

#define TP_I2C_ADDR		(0x40)

#define TP_I2C_CHL		(SOFT_I2C_CH1)

#define TP_I2C_BurstReadBytes(x,y,z,w,v)					I2C_BurstReadBytes_3(x,y,z,w,v)
#define TP_I2C_BurstWriteBytes(x,y,z,w,v)					I2C_BurstWriteBytes_3(x,y,z,w,v)
#define TP_I2C_BurstWriteBytes_ViaReadTable(x,y,z,w,v)	I2C_BurstWriteBytes_ViaReadTable(x,y,z,w,v)
#define TP_I2C_BurstReadBytes_MxtDma(x,y,z,w,v,r,s)		I2C_BurstReadBytes_MxtDma(x,y,z,w,v,r,s)

static bool Is_TP_Touch_Int(void);
static bool Read_Touch_Data(void);
static void Set_TP_Int_Port(void);

bool Init_TP_Driver(void)
{
	bool ret = FALSE;

	Set_TP_Int_Port();

	Set_TP_Driver_To_Normal();

	i2cIO_Init(TP_I2C_CHL);

	return ret;
}

void Set_TP_Driver_To_Normal(void)
{
	IO_CTP_RESET_VAL = 1u;
	IO_CTP_RESET_DIR = 1u;
}

void TP_Driver_To_Reset(void)
{
	IO_CTP_RESET_VAL = 0u;
	IO_CTP_RESET_DIR = 1u;	
}

static void Set_TP_Int_Port(void)
{
	IO_CTP_INT_DIR = 0u;
}

uint16 x_pos;
uint16 y_pos;

uint8 s_cnt = 0;
bool Try_Read_TP_Touch_Data(uint8 *tpCnt)
{
	if(TRUE == Is_TP_Touch_Int())
	{
		x_pos = 0;
		y_pos = 0;
		if(FALSE == Read_Touch_Data())
		{
			s_cnt++;
			*tpCnt = s_cnt;
			return TRUE;
		}
		return FALSE;
	}
	else
	{
		//cnt = 0;
		return FALSE;
	}
}

static bool Is_TP_Touch_Int(void)
{
	bool ret = FALSE;
	if(IO_CTP_INT_VAL_R)
		ret = FALSE;
	else
		ret = TRUE;
	return ret;
}



static bool Read_Touch_Data(void)
{
	#define I2C_SEND_BUF_LEN	(10)
	#define I2C_RECV_BUF_LEN	(0x7F)	

	int8 ret = FALSE;
	int8 intRegData = 0;
	uint8 sendBuf[I2C_SEND_BUF_LEN] = {0};
	uint8 readBuf[I2C_RECV_BUF_LEN] = {0};
	uint8 posBuf[I2C_RECV_BUF_LEN] = {0};

	sendBuf[0] = 0xFF;
	sendBuf[1] = 0x00;
	ret = I2C_BurstWriteBytes(SOFT_I2C_CH1,
							TP_I2C_ADDR,
							sendBuf,
							2);

	if(TRUE == ret)
	{
		sendBuf[0] = 0x06;
		ret = I2C_BurstWriteBytes(SOFT_I2C_CH1,
							TP_I2C_ADDR,
							sendBuf,
							1);
	}

	if(TRUE == ret)
	{
		ret = I2C_BurstReadBytes(SOFT_I2C_CH1,
								TP_I2C_ADDR,
								readBuf, 
								2);
	}

	/*if(TRUE == ret)
	{
		if(readBuf[0] >> 0x7)
		{
			sendBuf[0] = 0x0C;
			sendBuf[1] = 0x80;			
			ret = I2C_BurstWriteBytes(SOFT_I2C_CH1,
								TP_I2C_ADDR,
								sendBuf,
								2);
		}
	}*/
	
	if(TRUE == ret)
	{
		sendBuf[0] = 0x08;
		ret = I2C_BurstWriteBytes(SOFT_I2C_CH1,
								TP_I2C_ADDR,
								sendBuf,
								1); 
	}
	if(TRUE == ret)
	{
		ret = I2C_BurstReadBytes(SOFT_I2C_CH1,
								TP_I2C_ADDR,
								posBuf, 
								83);		
	}

	x_pos = ((uint16)(posBuf[2])<<8) + (uint16)(posBuf[1]);
	y_pos = ((uint16)(posBuf[4])<<8) + (uint16)(posBuf[3]);

	if(1 == posBuf[0])
		return TRUE;
	else
		return FALSE;
	//PrintF("TP finger state:%d,",posBuf[0]);
	//PrintF("x = %d,",x_pos);
	//PrintF("y = %d\n",y_pos);	
#if 0
	// clear int pin
	if(TRUE == ret)
	{
		intRegData = I2C_ReadByte(SOFT_I2C_CH1,
					TP_I2C_ADDR,
					0x07,
					&ret);
		#if 0
		ret = I2C_Read_Byte_With_RegAddr(SOFT_I2C_CH1,
					TP_I2C_ADDR,
					0x07,
					&intRegData,
					1);
		#endif
	}

	// end clear int pin	
	
	if(TRUE == ret)
	{
		sendBuf[0] = 0xFF;
		sendBuf[1] = 0x01;
		ret = I2C_BurstWriteBytes(SOFT_I2C_CH1,
								TP_I2C_ADDR,
								sendBuf,
								2);
	
	}
	if(TRUE == ret)
	{
		sendBuf[0] = 0x00;
		ret = I2C_BurstWriteBytes(SOFT_I2C_CH1,
							TP_I2C_ADDR,
							sendBuf,
							1);
	}	
	if(TRUE == ret)
	{
		ret = I2C_BurstReadBytes(SOFT_I2C_CH1,
								TP_I2C_ADDR,
								readBuf, 
								4);
	}	
	if(TRUE == ret)
	{
		sendBuf[0] = 0x04;
		ret = I2C_BurstWriteBytes(SOFT_I2C_CH1,
							TP_I2C_ADDR,
							sendBuf,
							1);
	}	
	if(TRUE == ret)
	{
		ret = I2C_BurstReadBytes(SOFT_I2C_CH1,
								TP_I2C_ADDR,
								readBuf, 
								26);		
	}
#endif
}

